package com.kuanter.kuanterauto.activity;

import java.io.IOException;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kuanter.auto.sns.CallBack;
import com.kuanter.auto.sns.SNS;
import com.kuanter.auto.sns.TX;
import com.kuanter.auto.sns.WX;
import com.kuanter.auto.sns.XL;
import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.umeng.analytics.MobclickAgent;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.net.RequestListener;

/**
 * 分享页面
 * @author kuanter
 *
 */
public class ShareActivity extends BaseActivity_NoBar implements CallBack{

	Button left_back;
	TextView top_middle;
	EditText share_merchan;
	
	TextView syzs;
	
	String address;
	String phone;
	String name;
	String currentService;
	String currentJifen;
	String merchanImg;
	
	SNS xl;
	SNS tx;
	WX wx;
	
	CharSequence shareText;
	
	String saveMoney;
	
	int status;  //0表示腾讯，1表示新浪、2表示微信
	
	int who;
	private String title;
	private String pushTime;
	private String content;
	private String shop;
	private String consumer_type;
	private String pay;
	
	public static final int MY = 10;
	public static final int HE = 20;
	public static final int YOU = 30;
	public static final int IT = 40;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.share);
		Intent intent = getIntent();
		who = intent.getIntExtra("who", -1);
		if(who == MY) {
			address = intent.getStringExtra("address");
			phone = intent.getStringExtra("phone");
			name = intent.getStringExtra("name");
			status = intent.getIntExtra("status", 0);
			currentJifen = intent.getStringExtra("currentJifen");
			currentService = intent.getStringExtra("currentService");
			merchanImg = intent.getStringExtra("merchanImg");
		} else if(who == HE){
			status = intent.getIntExtra("status", 0);
			saveMoney = intent.getStringExtra("saveMoney");
		}else if(who == YOU){
			status = intent.getIntExtra("status", 0);
			title = intent.getStringExtra("title");
			pushTime = intent.getStringExtra("pushTime");
			content = intent.getStringExtra("content");
		}else if(who == IT){
			status = intent.getIntExtra("status", 0);
			shop = intent.getStringExtra("shop");
			consumer_type = intent.getStringExtra("consumer_type");
			pay = intent.getStringExtra("pay");
		}
		initView();
		
		if(status == 0) {
			tx = new TX(this,MainConstants.TX_KEY,MainConstants.TX_SCREAT,"null");
			tx.regCallback(this);
		} else if(status == 1) {
			xl = new XL(this,MainConstants.XL_KEY,MainConstants.XL_URL);
			
			xl.regCallback(this);
		} else if(status == 2) {
			wx = new WX(this, MainConstants.WEIXIN_APP_ID);
			wx.regCallback(this);
		}
		
	}

	
	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}




	Handler handler = new Handler();

	public void confirm(View view) {
		if(status == 0) {
			showProgress("正在加载数据...");
			tx.oAuth();
				
			/*new Thread(new Runnable() {
				@Override
				public void run() {
					tx.oAuth();
				}
			});*/
		} else if(status == 1) {
			xl.oAuth();
		} else if(status == 2) {
			wx.oAuth();
		}
	}

	private void initView() {
		left_back = (Button) findViewById(R.id.left_back);
		left_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		top_middle = (TextView) findViewById(R.id.top_middle);
		top_middle.setText("分享好友");
		
		share_merchan = (EditText) findViewById(R.id.share_merchan);
		share_merchan.setFilters(new InputFilter[]{new InputFilter.LengthFilter(140)});
		if(who == MY) {
			share_merchan.setText("今天我又搜罗了一家好店，分享给大家， @宽途汽车 " + name + "，服务项目：" + currentService + "，仅用："
			+ currentJifen + "，位于：" + address + "! 有宽途卡的朋友可以去试试!");
		} else if(who == HE) {
			share_merchan.setText("原来 @宽途汽车 已经为我节省了"+saveMoney+"元啦！你也来用的试试吧！");
		}else if(who == YOU){
//			share_merchan.setFilters(new InputFilter[]{new InputFilter.LengthFilter(100000)});
//			share_merchan.setText(content+" @宽途汽车");
			share_merchan.setText(content);
		}else if(who == IT){
			share_merchan.setText("我在@宽途汽车  的"+shop +"刷卡消费了一次"+ consumer_type+"，刷卡才"+pay+"，很划算，推荐给大家！");
		}
		share_merchan.setSelection(share_merchan.getText().toString().length());
		
		syzs = (TextView) findViewById(R.id.syzs);
		int sy = 140 - share_merchan.getText().toString().length();
		syzs.setText("还可输入" + sy + "字");
		
		share_merchan.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				shareText = s;
				if(shareText.length() >= 140) {
					
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				syzs.setText("还可输入" + (140 - shareText.length()) + "字");
			}
		});
		
	}




	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void toShare(int arg0) {
		if(arg0 == CallBack.WHO_TX) {
			handler.post(new Runnable() {
				@Override
				public void run() {
					boolean success = tx.share(share_merchan.getText().toString(),null,true);
					if(success) {
						showToast("分享成功");
						finish();
					} else {
						showToast("分享失败");
					}
					finishProgress();
				}
			});
			
		} else if(arg0 == CallBack.WHO_XL) {
			xl.share(share_merchan.getText().toString(), new RequestListener() {
				@Override
				public void onIOException(IOException arg0) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							showToast("分享失败");
						}
					});
					
				}
				
				@Override
				public void onError(WeiboException arg0) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							showToast("分享失败");
						}
					});
				}
				
				@Override
				public void onComplete(String arg0) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							showToast("分享成功");
							finish();
						}
					});
				}
			},true);
		} else if(arg0 == CallBack.WHO_WX) {
			if(wx.isInstall()) {
				boolean flag = false;
				if(wx.apiSupport()) {
					flag = true;
				}
				boolean success = wx.share(share_merchan.getText().toString(), null, flag);
				if(success) {
//					showToast("分享成功");
					Log.i("ccc", success + "aaaaaaaaaaaaaaa");
//					finish();
				} else {
					showToast("分享失败");
				}
			} else {
				showToast("您没有安装微信");
			}
		}
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		finishProgress();
		try {
			tx.activityResult(requestCode, resultCode, data);
		} catch(Exception e) {
			e.printStackTrace();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	public void exit(View view) {
		finish();
	}
	
}
